from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score

# 1. 加载鸢尾花数据集
iris = datasets.load_iris()
X = iris.data  # 特征数据
y = iris.target  # 标签数据

# 2. 数据划分：70% 训练集，30% 测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 3. 创建 SVM 模型并训练
svm_model = SVC(kernel='linear')  # 使用线性核的支持向量机
svm_model.fit(X_train, y_train)

# 4. 用测试集进行预测
y_pred = svm_model.predict(X_test)

# 5. 输出分类报告和准确率
print("分类报告：")
print(classification_report(y_test, y_pred))

print(f"准确率: {accuracy_score(y_test, y_pred) * 100:.2f}%")
